/*

Copyright (c) 2001, 2002, 2003 Flo Ledermann <flo@subnet.at>

This file is part of parvis - a parallel coordiante based data visualisation
tool written in java. You find parvis and additional information on its
website at http://www.mediavirus.org/parvis.

parvis is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

parvis is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with parvis (in the file LICENSE.txt); if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

package org.mediavirus.parvis.gui;

import javax.swing.*;
import java.awt.Color;

/**
 *
 * @author  flo
 */
public class PrefsFrame extends javax.swing.JFrame {
    
    ParallelDisplay parent;
    
    /** Creates new form PrefsFrame */
    public PrefsFrame(ParallelDisplay parent) {
        this.parent = parent;
        
        initComponents();
        
        updatePrefs();
    }
    
    private void updatePrefs(){
        binsField.setText(parent.getIntPreference("histogramBins") + "");
        
        switch (parent.getIntPreference("histogramWidth")){
            case ParallelDisplay.HISTO_TOTALREC:
                totalRadio.setSelected(true);
                break;
            case ParallelDisplay.HISTO_BINREC:
                binRadio.setSelected(true);
                break;
            case ParallelDisplay.HISTO_BRUSHREC:
                brushedRadio.setSelected(true);
                break;
        }
        
        bgColButton.setBackground(parent.getBackground());
        recordColButton.setBackground(parent.getColorPreference("recordColor"));
        axisColButton.setBackground(parent.getForeground());
        brushColButton.setBackground(parent.getColorPreference("brushColor"));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        histoWidthGroup = new javax.swing.ButtonGroup();
        colorPanel = new javax.swing.JPanel();
        label1 = new javax.swing.JLabel();
        bgColButton = new javax.swing.JButton();
        label2 = new javax.swing.JLabel();
        recordColButton = new javax.swing.JButton();
        label3 = new javax.swing.JLabel();
        axisColButton = new javax.swing.JButton();
        label31 = new javax.swing.JLabel();
        brushColButton = new javax.swing.JButton();
        histogramPanel = new javax.swing.JPanel();
        label32 = new javax.swing.JLabel();
        binsField = new javax.swing.JTextField();
        label33 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        totalRadio = new javax.swing.JRadioButton();
        binRadio = new javax.swing.JRadioButton();
        brushedRadio = new javax.swing.JRadioButton();

        getContentPane().setLayout(new java.awt.GridBagLayout());

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        colorPanel.setLayout(new java.awt.GridBagLayout());

        colorPanel.setBorder(new javax.swing.border.TitledBorder(new javax.swing.border.EtchedBorder(), "Colors"));
        label1.setFont(new java.awt.Font("Dialog", 0, 11));
        label1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        label1.setText("Background");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 2, 4);
        colorPanel.add(label1, gridBagConstraints);

        bgColButton.setBackground(new java.awt.Color(255, 255, 255));
        bgColButton.setFont(new java.awt.Font("Dialog", 1, 3));
        bgColButton.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2));
        bgColButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bgColButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        bgColButton.setMaximumSize(new java.awt.Dimension(10, 10));
        bgColButton.setMinimumSize(new java.awt.Dimension(10, 10));
        bgColButton.setPreferredSize(new java.awt.Dimension(14, 14));
        bgColButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        colorPanel.add(bgColButton, gridBagConstraints);

        label2.setFont(new java.awt.Font("Dialog", 0, 11));
        label2.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        label2.setText("Records");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 4, 2, 4);
        colorPanel.add(label2, gridBagConstraints);

        recordColButton.setBackground(new java.awt.Color(0, 0, 0));
        recordColButton.setFont(new java.awt.Font("Dialog", 1, 3));
        recordColButton.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2));
        recordColButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        recordColButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        recordColButton.setMaximumSize(new java.awt.Dimension(10, 10));
        recordColButton.setMinimumSize(new java.awt.Dimension(10, 10));
        recordColButton.setPreferredSize(new java.awt.Dimension(14, 14));
        recordColButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        colorPanel.add(recordColButton, gridBagConstraints);

        label3.setFont(new java.awt.Font("Dialog", 0, 11));
        label3.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        label3.setText("Axes & Labels");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 4, 2, 4);
        colorPanel.add(label3, gridBagConstraints);

        axisColButton.setBackground(new java.awt.Color(0, 0, 0));
        axisColButton.setFont(new java.awt.Font("Dialog", 1, 3));
        axisColButton.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2));
        axisColButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        axisColButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        axisColButton.setMaximumSize(new java.awt.Dimension(10, 10));
        axisColButton.setMinimumSize(new java.awt.Dimension(10, 10));
        axisColButton.setPreferredSize(new java.awt.Dimension(14, 14));
        axisColButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        colorPanel.add(axisColButton, gridBagConstraints);

        label31.setFont(new java.awt.Font("Dialog", 0, 11));
        label31.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        label31.setText("Default Brush");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(2, 4, 2, 4);
        colorPanel.add(label31, gridBagConstraints);

        brushColButton.setBackground(new java.awt.Color(0, 0, 0));
        brushColButton.setFont(new java.awt.Font("Dialog", 1, 3));
        brushColButton.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2));
        brushColButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        brushColButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        brushColButton.setMaximumSize(new java.awt.Dimension(10, 10));
        brushColButton.setMinimumSize(new java.awt.Dimension(10, 10));
        brushColButton.setPreferredSize(new java.awt.Dimension(14, 14));
        brushColButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        colorPanel.add(brushColButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(colorPanel, gridBagConstraints);

        histogramPanel.setLayout(new java.awt.GridBagLayout());

        histogramPanel.setBorder(new javax.swing.border.TitledBorder(new javax.swing.border.EtchedBorder(), "Histogram"));
        label32.setFont(new java.awt.Font("Dialog", 0, 11));
        label32.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        label32.setText("Bins: ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        histogramPanel.add(label32, gridBagConstraints);

        binsField.setFont(new java.awt.Font("Dialog", 0, 10));
        binsField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        binsField.setText("10");
        binsField.setPreferredSize(new java.awt.Dimension(36, 18));
        binsField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                binsFieldActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        histogramPanel.add(binsField, gridBagConstraints);

        label33.setFont(new java.awt.Font("Dialog", 0, 11));
        label33.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        label33.setText("Width: ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        histogramPanel.add(label33, gridBagConstraints);

        jPanel1.setLayout(new java.awt.GridLayout(3, 1));

        totalRadio.setFont(new java.awt.Font("Dialog", 0, 10));
        totalRadio.setSelected(true);
        totalRadio.setText("total # of records");
        histoWidthGroup.add(totalRadio);
        totalRadio.setMargin(new java.awt.Insets(0, 0, 0, 0));
        totalRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                binWidthActionPerformed(evt);
            }
        });

        jPanel1.add(totalRadio);

        binRadio.setFont(new java.awt.Font("Dialog", 0, 10));
        binRadio.setText("max # in bin");
        histoWidthGroup.add(binRadio);
        binRadio.setMargin(new java.awt.Insets(0, 0, 0, 0));
        binRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                binWidthActionPerformed(evt);
            }
        });

        jPanel1.add(binRadio);

        brushedRadio.setFont(new java.awt.Font("Dialog", 0, 10));
        brushedRadio.setText("max brushed in bin");
        histoWidthGroup.add(brushedRadio);
        brushedRadio.setMargin(new java.awt.Insets(0, 0, 0, 0));
        brushedRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                binWidthActionPerformed(evt);
            }
        });

        jPanel1.add(brushedRadio);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        histogramPanel.add(jPanel1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(histogramPanel, gridBagConstraints);

        pack();
    }//GEN-END:initComponents

    private void colButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colButtonActionPerformed

        Color current = bgColButton.getBackground();

        if (evt.getSource() == recordColButton) { 
            current = recordColButton.getBackground();
        }
        else if (evt.getSource() == axisColButton) { 
            current = axisColButton.getBackground();
        }
        else if (evt.getSource() == brushColButton) { 
            current = brushColButton.getBackground();
        } 

        // Bring up a color chooser 
        Color c = JColorChooser.showDialog(this, "Choose Color", current); 

        if (evt.getSource() == bgColButton) { 
            bgColButton.setBackground(c);
            parent.setBackground(c);
        }
        else if (evt.getSource() == recordColButton) { 
            recordColButton.setBackground(c);
            parent.setPreference("recordColor", c);
        }
        else if (evt.getSource() == axisColButton) { 
            axisColButton.setBackground(c);
            parent.setForeground(c);
        }
        else { 
            brushColButton.setBackground(c);
            parent.setPreference("brushColor", c);
        } 
    }//GEN-LAST:event_colButtonActionPerformed

    private void binsFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_binsFieldActionPerformed
        parent.setIntPreference("histogramBins", Integer.parseInt(binsField.getText()));
    }//GEN-LAST:event_binsFieldActionPerformed

    private void binWidthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_binWidthActionPerformed
        if (totalRadio.isSelected()) {
            parent.setIntPreference("histogramWidth", ParallelDisplay.HISTO_TOTALREC);
        }
        else if (binRadio.isSelected()) {
            parent.setIntPreference("histogramWidth", ParallelDisplay.HISTO_BINREC);
        }
        else if (brushedRadio.isSelected()) {
            parent.setIntPreference("histogramWidth", ParallelDisplay.HISTO_BRUSHREC);
        }
    }//GEN-LAST:event_binWidthActionPerformed
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm

    }//GEN-LAST:event_exitForm
        
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel label3;
    private javax.swing.JLabel label2;
    private javax.swing.JLabel label1;
    private javax.swing.JButton brushColButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton binRadio;
    private javax.swing.JButton axisColButton;
    private javax.swing.JRadioButton brushedRadio;
    private javax.swing.JPanel colorPanel;
    private javax.swing.JRadioButton totalRadio;
    private javax.swing.JLabel label33;
    private javax.swing.JLabel label32;
    private javax.swing.JLabel label31;
    private javax.swing.JTextField binsField;
    private javax.swing.JButton bgColButton;
    private javax.swing.ButtonGroup histoWidthGroup;
    private javax.swing.JButton recordColButton;
    private javax.swing.JPanel histogramPanel;
    // End of variables declaration//GEN-END:variables
    
}
